package com.baitai.system.service.impl;

import java.time.LocalDate;
import java.time.YearMonth;
import java.util.Date;
import java.util.List;

import com.baitai.common.core.domain.model.LoginUser;
import com.baitai.common.utils.DateUtils;
import com.baitai.common.utils.SecurityUtils;
import com.baitai.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baitai.system.mapper.UserSignInRecordsMapper;
import com.baitai.system.domain.UserSignInRecords;
import com.baitai.system.service.IUserSignInRecordsService;

/**
 * 签到记录Service业务层处理
 *
 * @author xx
 * @date 2024-07-21
 */
@Service
public class UserSignInRecordsServiceImpl implements IUserSignInRecordsService {
    @Autowired
    private UserSignInRecordsMapper userSignInRecordsMapper;

    /**
     * 查询签到记录
     *
     * @param id 签到记录主键
     * @return 签到记录
     */
    @Override
    public UserSignInRecords selectUserSignInRecordsById(Long id) {
        return userSignInRecordsMapper.selectUserSignInRecordsById(id);
    }

    /**
     * 查询签到记录列表
     *
     * @param userSignInRecords 签到记录
     * @return 签到记录
     */
    @Override
    public List<UserSignInRecords> selectUserSignInRecordsList(UserSignInRecords userSignInRecords) {
        return userSignInRecordsMapper.selectUserSignInRecordsList(userSignInRecords);
    }

    /**
     * 新增签到记录
     *
     * @param userSignInRecords 签到记录
     * @return 结果
     */
    @Override
    public int insertUserSignInRecords(UserSignInRecords userSignInRecords) {
        userSignInRecords.setCreateTime(DateUtils.getNowDate());
        return userSignInRecordsMapper.insertUserSignInRecords(userSignInRecords);
    }

    /**
     * 修改签到记录
     *
     * @param userSignInRecords 签到记录
     * @return 结果
     */
    @Override
    public int updateUserSignInRecords(UserSignInRecords userSignInRecords) {
        userSignInRecords.setUpdateTime(DateUtils.getNowDate());
        return userSignInRecordsMapper.updateUserSignInRecords(userSignInRecords);
    }

    /**
     * 批量删除签到记录
     *
     * @param ids 需要删除的签到记录主键
     * @return 结果
     */
    @Override
    public int deleteUserSignInRecordsByIds(Long[] ids) {
        return userSignInRecordsMapper.deleteUserSignInRecordsByIds(ids);
    }

    /**
     * 删除签到记录信息
     *
     * @param id 签到记录主键
     * @return 结果
     */
    @Override
    public int deleteUserSignInRecordsById(Long id) {
        return userSignInRecordsMapper.deleteUserSignInRecordsById(id);
    }

    /**
     * 查询签到记录列表
     *
     * @return 签到记录
     */
    @Override
    public List<UserSignInRecords> appList(Long userId) {
        Date start = DateUtils.getStartOfMonth();
        Date end = DateUtils.getEndOfMonth();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isNotEmpty(loginUser.getUser().getIsPrivate()) && "2".equals(loginUser.getUser().getIsPrivate())) {
            return userSignInRecordsMapper.appListPrivate(userId, start, end);
        } else {
            return userSignInRecordsMapper.appList(userId, start, end);
        }
    }

    @Override
    public void signIn(Long userId, String numberOfViews, String continuousDays, String viewsSuccessPoints, String additionalPoints, String isPrivate) {
        Date start = DateUtils.startOfDay();
        Date end = DateUtils.endOfDay();
        UserSignInRecords userSignInRecords = userSignInRecordsMapper.querySignInRecords(userId, start, end);
        if (userSignInRecords != null) {
            userSignInRecords.setStatus("1");
            userSignInRecords.setNumberOfViews(numberOfViews);
            userSignInRecords.setContinuousDays(continuousDays);
            userSignInRecords.setViewsSuccessPoints(viewsSuccessPoints);
            userSignInRecords.setAdditionalPoints(additionalPoints);
            userSignInRecords.setSignInDate(new Date());
//            if(StringUtils.isNotEmpty(isPrivate)&&"1".equals(isPrivate)){
//                userSignInRecordsMapper.updateUserSignInRecordsPrivate(userSignInRecords);
//            }else {
            userSignInRecordsMapper.updateUserSignInRecords(userSignInRecords);
//            }
        } else {
            userSignInRecords = new UserSignInRecords();
            userSignInRecords.setUserId(userId);
            userSignInRecords.setStatus("1");
            userSignInRecords.setNumberOfViews(numberOfViews);
            userSignInRecords.setContinuousDays(continuousDays);
            userSignInRecords.setViewsSuccessPoints(viewsSuccessPoints);
            userSignInRecords.setAdditionalPoints(additionalPoints);
            userSignInRecords.setSignInDate(new Date());
            userSignInRecords.setCreateTime(DateUtils.getNowDate());
//            if(StringUtils.isNotEmpty(isPrivate)&&"1".equals(isPrivate)){
//                userSignInRecordsMapper.insertUserSignInRecordsPrivate(userSignInRecords);
//            }else {
            userSignInRecordsMapper.insertUserSignInRecords(userSignInRecords);
//            }
        }
    }
}
